import { withRouter } from 'react-router-dom'
import { Form, Input, Button, Result } from 'antd';
import Layout from '../../components/layout'
import $http from '../../utils/require';
import rules from '../../utils/rules'
import React, { useState } from 'react';
import { Res } from '../../types/base';
import stores from '../../stores';


const SignIn = (props: any) => {
    console.log('SignIn', props);
    let [form] = Form.useForm()
    let [result, setResult]: [Res, Function] = useState({})

    const onFinish = (values: any) => {
        console.log('Success:', values);
        let { email, password } = values

        $http['signin']({
            email,
            password
        }).then(res => {
            stores.User.setLocalInfo(res)
            form.resetFields()
            console.log('role', res.user.role);

            // props.history.push(res.user.role === 1 ? '/admin/dashboard' : '/user/dashboard')
            props.history.push('/admin/dashboard')
        }).catch(err => {
            setResult(err)
        })
    };


    return (
        <Layout title="登录" subtitle="嘿，小伙计，赶紧登录你的电商系统吧">
            {
                result.error ? <Result
                    status='warning'
                    title={result.error}
                /> : <></>
            }

            <Form
                onFinish={onFinish}
                form={form}>

                <Form.Item name="email" label="邮箱" rules={rules.email}>
                    <Input></Input>
                </Form.Item>
                <Form.Item name="password" label="密码" rules={rules.password}>
                    <Input.Password></Input.Password>
                </Form.Item>

                <Button type="primary" htmlType="submit">登录</Button>
            </Form>
        </Layout>
    )
}

export default withRouter(SignIn)
